//
//  LifeSeviceSubCell.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/16.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "LifeSeviceSubCell.h"
#import "LifeSeviceItemCell.h"
#import "LifeSevicItemLayout.h"
#import "RentHouseDetailVC.h"
#import "GoodDetailController.h"
#import "EmploymentDetailVC.h"

#import "AboutusController.h"
@interface LifeSeviceSubCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionVeiw;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomMargin;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewH;
@property (weak, nonatomic) IBOutlet UIView *markView;
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;

@end




@implementation LifeSeviceSubCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.contentView.backgroundColor = viewBgGrey;
    [self.myCollectionVeiw registerNib:[UINib nibWithNibName:NSStringFromClass([LifeSeviceItemCell class]) bundle:nil] forCellWithReuseIdentifier:[LifeSeviceItemCell identifierStr]];
    
    self.myCollectionVeiw.showsVerticalScrollIndicator = false;
    self.myCollectionVeiw.showsHorizontalScrollIndicator = false;
    self.myCollectionVeiw.scrollEnabled = NO;
    
    self.myCollectionVeiw.backgroundColor = lineGray;
    
    self.myCollectionVeiw.delegate = self;
    
    self.myCollectionVeiw.dataSource = self;
    
    self.bottomMargin.constant = 0;
    
    
    self.markView.backgroundColor = colorWith(35, 122, 217);
    
    self.markView.layer.cornerRadius = 1;
    
    self.markView.layer.masksToBounds = YES;
    
    self.rightLabel.font = MYFONT(16);
}

- (void)setModels:(NSArray<HomeSubModel *> *)models{
    _models = models;
    
    UIImage *image0 = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:_models.firstObject.adv_image];
    
//    NSLog(@"%f",image0.size.width);
//    NSLog(@"%f",image0.size.height);

    LifeSevicItemLayout *layout = (LifeSevicItemLayout *)self.myCollectionVeiw.collectionViewLayout;
    
    CGFloat imageW0 = (KScreenW - 0.8) * 0.5;
    
    CGFloat tempH = imageW0 / 375.0 * 182.0;
    
    self.collectionViewH.constant = tempH;
    
    
    layout.itemSize = CGSizeMake(imageW0, tempH);
    
    if (image0) {
        
        CGFloat imageViewH = imageW0 / image0.size.width * image0.size.height;
        // 重置放大缩小的比例
        self.collectionViewH.constant = imageViewH;
    
        layout.itemSize = CGSizeMake(imageW0, imageViewH);
        
        [self.myCollectionVeiw reloadData];
        
    } else {
        
        MJWeakSelf;
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:_models.firstObject.adv_image] options:kNilOptions progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
            if (image) {
                
                CGFloat imageW = (KScreenW - 0.8) * 0.5;
                
                CGFloat imageViewH = imageW / image.size.width * image.size.height;
                // 重置放大缩小的比例
                weakSelf.collectionViewH.constant = imageViewH;
                
                
                layout.itemSize = CGSizeMake(imageW, imageViewH);
                
                
                [weakSelf.myCollectionVeiw reloadData];
                
            }
            
            
        }];
    }
    
    
    
    
}




- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    
    return 1;
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.models.count;
    
    
    
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    LifeSeviceItemCell *temp = (LifeSeviceItemCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[LifeSeviceItemCell identifierStr] forIndexPath:indexPath];
    
    temp.model = self.models[indexPath.item];
    
    return temp;
    
    
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeSubModel *subModel = self.models[indexPath.item];
    
    if ([subModel.class_id isEqualToString:@"1"]) {
        
        RentHouseDetailVC *temp = [[RentHouseDetailVC alloc]init];
        
        temp.houseId = subModel.ID;
        
        [self.viewController.navigationController pushViewController:temp animated:YES];
        
        
    } else if ([subModel.class_id isEqualToString:@"2"]){
        
        EmploymentDetailVC *tempVC = [[EmploymentDetailVC alloc]init];
        
        tempVC.IdStr = subModel.ID;
        
        [self.viewController.navigationController pushViewController:tempVC animated:YES];

    }else if ([subModel.class_id isEqualToString:@"3"]){
        
        GoodDetailController *tempVC = [[GoodDetailController alloc]init];
        
        OneGoodsModel *tempModel = [[OneGoodsModel alloc]init];
        
        tempModel.goods_id = subModel.ID;
        
        tempModel.goods_image_url = subModel.adv_image;
        
        tempVC.goodsModel = tempModel;
        
        [self.viewController.navigationController pushViewController:tempVC animated:YES];

        
    }else{
        
        AboutusController *temp = [[AboutusController alloc]init];
        
        temp.urlString = subModel.content;
        
        [self.viewController.navigationController pushViewController:temp animated:YES];
        
    }
    
    
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
